package com.winhxd.b2c.order.controller;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.order.condition.*;
import com.winhxd.b2c.common.domain.order.vo.OrderAfterSaleDetailVO;
import com.winhxd.b2c.common.domain.order.vo.OrderAfterSaleVO;
import com.winhxd.b2c.common.exception.BusinessException;
import com.winhxd.b2c.common.feign.order.OrderAfterSaleClient;
import com.winhxd.b2c.common.util.JsonUtil;
import com.winhxd.b2c.order.service.OrderAfterSaleDetailService;
import com.winhxd.b2c.order.service.OrderAfterSaleService;
import com.winhxd.b2c.order.service.OrderExpressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


/**
 * @program: hxd-outlet-parent
 * @description: 订单售后审核信息查询-后台接口
 * @author: zhanghuan
 * @create: 2019-01-07 15:55
 **/
@Api("OrderAfterSale")
@RestController
public class OrderAfterSaleController implements OrderAfterSaleClient {
	private static final Logger logger = LoggerFactory.getLogger(OrderAfterSaleController.class);

	@Autowired
	private OrderAfterSaleService orderAfterSaleService;

	@Autowired
	private OrderExpressService expressService;

	@Autowired
	private OrderAfterSaleDetailService orderAfterSaleDetailService;

	@Override
	@ApiOperation(value = "售后列表按钮点击操作对应的信息查询--后台接口", response = OrderAfterSaleVO.class, notes = "售后列表按钮点击操作对应的信息查询")
	@ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType= "OrderAfterSaleCondition", name = "condition", value = "售后列表按钮点击操作对应的信息查询")})
	public ResponseResult<OrderAfterSaleVO> checkOrderAfterSale(@RequestBody OrderAfterSaleCondition condition) {
		String logTitle ="/afterApplication/4601/v1/check/";
		logger.info(logTitle+"{} 订单售后审核按钮操作查询,入参：", condition);
		ResponseResult<OrderAfterSaleVO> result = new ResponseResult<>();
		OrderAfterSaleVO orderAfterSaleVO = orderAfterSaleService.getCheckOrderAfterSale(condition);
		result.setData(orderAfterSaleVO);
		return result;
	}

	@Override
	@ApiOperation(value = "售后信息列表", response = OrderAfterSaleVO.class, notes = "售后信息列表")
	@ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType= "OrderAfterSaleListCondition", name = "condition", value = "售后信息列表")})
	public ResponseResult<PagedList<OrderAfterSaleDetailVO>> orderAfterSaleList(@RequestBody OrderAfterSaleListCondition condition) {
		String logTitle = "/afterApplication/4618/v1/list/";
		logger.info(logTitle+"{} 订单售后审核信息查询,入参：", condition);
		ResponseResult<PagedList<OrderAfterSaleDetailVO>> result = new ResponseResult<>();
		PagedList<OrderAfterSaleDetailVO> detailList = orderAfterSaleService.findOrderAfterSaleDetailList(condition);
		result.setData(detailList);
		return result;
	}


	@Override
	@ApiOperation(value = "审核售后订单", response = Integer.class, notes = "审核售后订单")
	public ResponseResult<Integer> checkAfterSale(@RequestBody OrderAfterSaleCheckCondition condition) {
		String logTitle = "/afterSale/4602/v1/checkAfterSale-审核售后订单";
		if(null == condition){
			logger.error(logTitle+"--审核售后订单 入参为空");
			throw new BusinessException(BusinessCode.CODE_406101);
		}
		ResponseResult<Integer> result = new ResponseResult<>();
		logger.info("{}=--开始--{}",logTitle, JsonUtil.toJSONString(condition));
		int res = orderAfterSaleService.modifyAfterSaleOrderByStatus(condition);
		result.setData(res);
		return result;
	}

	@Override
	@ApiOperation(value = "售后订单退款", response = Integer.class, notes = "售后订单退款")
	public ResponseResult<Integer> saveRefundInfo(@RequestBody OrderAfterSaleRefundCondition condition) {
		String logTitle = "/afterSale/4603/v1/saveRefundInfo-售后订单退款";
		if(null == condition){
			logger.error(logTitle+"--售后退款订单 入参为空");
			throw new BusinessException(BusinessCode.CODE_406101);
		}
		ResponseResult<Integer> result = new ResponseResult<>();
		logger.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
		int res = orderAfterSaleService.saveRefundInfo(condition);
		result.setData(res);
		return result;
	}

	@Override
	@ApiOperation(value = "售后订单已发货换货", response = Integer.class, notes = "售后订单已发货换货")
	public ResponseResult<Integer> saveExpressInfo4DeliveredGoods(@RequestBody OrderExpressCondition condition) {
		String logTitle = "/afterSale/4604/v1/saveExpressInfo4DeliveredGoods-售后订单已发货换货";
		if(null == condition){
			logger.error(logTitle+"--售后订单已发货换货 入参为空");
			throw new BusinessException(BusinessCode.CODE_406101);
		}
		//校验参数
		this.validateParams(condition);
		ResponseResult<Integer> result = new ResponseResult<>();
		logger.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
		int res = expressService.saveExpress4Exchange(condition);
		result.setData(res);
		return result;
	}



	@Override
	@ApiOperation(value = "售后订单已完成换货", response = Integer.class, notes = "售后订单已完成换货")
	public ResponseResult<Integer> saveExpressInfo4Finished(@RequestBody OrderExpressCondition condition) {
		String logTitle = "/afterSale/4605/v1/saveExpressInfo4Finished-售后订单已完成换货";
		if(null == condition){
			logger.error(logTitle+"--售后订单已完成换货 入参为空");
			throw new BusinessException(BusinessCode.CODE_406101);
		}
		//校验参数
		this.validateParams(condition);
		ResponseResult<Integer> result = new ResponseResult<>();
		logger.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
		int res = expressService.saveExpress4Exchange(condition);
		result.setData(res);
		return result;
	}

	@Override
	@ApiOperation(value = "售后-手动关闭售后", response = Integer.class, notes = "售后-手动关闭售后")
	public ResponseResult<Void> closeAfterSale(@RequestBody OrderCloseAfterSaleCondition condition) {
		String logTitle = "/afterSale/4606/v1/closeAfterSale-售后-手动关闭售后";
		if(null == condition){
			logger.error(logTitle+"--售后-手动关闭售后 入参为空");
			throw new BusinessException(BusinessCode.CODE_406101);
		}
		if(null == condition.getId()){
			logger.error(logTitle+"--售后-手动关闭售后 入参为空");
			throw new BusinessException(BusinessCode.CODE_460019);
		}
		logger.info("{}=--开始--{}", logTitle, JsonUtil.toJSONString(condition));
		ResponseResult<Void> result = new ResponseResult<>();
		try{
			orderAfterSaleDetailService.closeAfterSale(condition.getId(),condition.getCustomerId());
		} catch (BusinessException e) {
			logger.error("手动关闭售后-业务异常：" + e.getMessage(), e);
			result.setCode(e.getErrorCode());
			throw e;
		} catch (Exception e) {
			logger.error("手动关闭售后-服务器内部错误：" + e.getMessage(), e);
			result.setCode(BusinessCode.CODE_1001);
			throw e;
		}
		return result;
	}

	/**
	 * 校验售后退换货物流信息入参
	 * @param condition
	 */
	private void validateParams(OrderExpressCondition condition){
		if(null == condition.getAfterSaleDetailId()){
			throw new BusinessException(BusinessCode.CODE_460019);
		}
		if(null == condition.getOrderNo()||"".equals(condition.getOrderNo())){
			throw new BusinessException(BusinessCode.CODE_460020);
		}
		if(null == condition.getSkuId()){
			throw new BusinessException(BusinessCode.CODE_460021);
		}
		if(null == condition.getExpressType()){
			throw new BusinessException(BusinessCode.CODE_460022);
		}
	}
}
